const AD = require('ym-AD')
const DataMgr = require('ym-Data')
const Events = require('ym-Events')
cc.Class({
    extends: require('UI-PanelWindow'),

    properties: {
        coinLabel : cc.Label,
        progress : cc.ProgressBar,
        scaleLabel : cc.Label
    },

    start(){
        this.power = 0;   
        this.isClickPage = true;
        this.progress.progress = 0
        this.scaleLabel.string = '0/10';

        CC_WECHATGAME && AD.showBanner('adunit-ebac3d1aa2469841')
        Events.on('bannershow', this.onBannerShow, this);
    },
    onBannerShow(size){
        this.bannerSize = size
        console.log('广告显示成功')
        AD.moveBanner(0, 0)
    },
    setOptions(options){
        this.options = options;
        this.coinLabel.string = options.coin;
    },
    onDestroy() {
        Events.removeAllListeners(this)
        AD.destroyBanner();
    },

    onClickSingle(){
        this.node.destroy();
        this.options.cb(1)
    },
    onClickDouble(){
        var self = this;
        var options={
            adUnitId:'adunit-2c296b5dd3cc98bb',
            stopCb:function(){
                global.EJMgr.showToast('视频未看完，不能获得奖励')
            },
            successCb:function(){
                if(self.node){
                    self.node.destroy();
                    self.options.cb(1)
                }
            }
        }
        
        global.WechatShare.videoOrShare(options)
    },
    onClickOpen(){
        this.power += 25;
        this.progress.progress = this.power / 100
        this.scaleLabel.string = Math.ceil(this.power / 10) + '/10';
        if(this.power >= 70){
            this.isClickPage = false;
            cc.find('views/page1',this.node).active = false;
            cc.find('views/page2',this.node).active = true;

            if(CC_WECHATGAME && this.bannerSize && !global.Data.isCheck){
                var systemInfo = wx.getSystemInfoSync();
                var y = this.bannerSize.height/systemInfo.screenHeight*cc.winSize.height
                AD.moveBanner(0, y)
            }
        }
    },
    update(dt){
        if(this.isClickPage){
            if (this.power > 0) {
                this.power -= 1
                this.progress.progress = this.power / 100
                this.scaleLabel.string = Math.ceil(this.power / 10) + '/10';
            }
        }
    }
});
